#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@desc: .

@Project：kuberCloudTest 
@File：spec.py
@Author：admin
@Date：2024/4/16 19:16 
"""

from kuberCloudTest.common.basic import basicUtils
from kuberCloudTest.common.constant import OFFSET, LIMIT, AsyncType
from kuberCloudTest.common.utils import _getOperationType
from kuberCloudTest.exception import KuberCloudException
from kuberCloudTest.networks.urls import networkUrls


class vpcRouterSpecUtils(basicUtils):

    def __init__(self, accessToken):
        super().__init__(accessToken)


def get_utils(accessToken):
    return vpcRouterSpecUtils(accessToken=accessToken)


def getVpcRouterSpec(accessToken, **kwargs):
    """
    查询VPC路由器规格信息
    :param accessToken:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVpcRouterSpec.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                         })


def createVpcRouterSpec(accessToken, specName, cpu: int, memory: int, memoryUnit: str,
                        routerMirrorId, publicNetworkId, managementNetworkId, remark=''):
    """
    创建VPC路由器规格
    :param accessToken:
    :param specName: VPC路由器规格名称
    :param cpu: CPU个数,范围[1-99]
    :param memory: 内存数值
    :param memoryUnit: 内存单位 G|M
    :param routerMirrorId: VPC路由器镜像UUID
    :param publicNetworkId: 公有网络UUID
    :param managementNetworkId: 管理网络UUID
    :param remark: 描述
    :return:
    """
    if memoryUnit not in ['G', 'M']:
        raise KuberCloudException('params error', 'memoryUnit must be G or M')
    operationType = 5601
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['rsname'] = specName
    data['remark'] = remark
    data['cpu'] = cpu
    data['memoryui'] = memory
    data['memoryuint'] = memoryUnit
    data['mirrorid'] = routerMirrorId
    data['publicnetworkid'] = publicNetworkId
    data['managenetworkid'] = managementNetworkId

    return get_utils(accessToken).cloud_http_request.post(networkUrls.createVpcRouterSpec.value, data)


def updateVpcRouterSpec(accessToken, vpcRouterSpecId, specName, remark):
    """
    更新VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecId: VPC路由器规格ID
    :param specName: VPC路由器规格名称
    :param remark: 描述
    :return:
    """
    operationType = 5602
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['rsname'] = specName
    data['remark'] = remark
    data['rsid'] = vpcRouterSpecId
    return get_utils(accessToken).cloud_http_request.post(networkUrls.updateVpcRouterSpec.value, data)


def stopVpcRouterSpec(accessToken, vpcRouterSpecIds: str):
    """
    停用VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecIds: VPC路由器规格ID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5605
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vpcRouterSpecIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.stopVpcRouterSpec.value, data)


def startVpcRouterSpec(accessToken, vpcRouterSpecIds: str):
    """
    启用VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecIds: VPC路由器规格ID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5604
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vpcRouterSpecIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.startVpcRouterSpec.value, data)


def globalShareVpcRouterSpec(accessToken, vpcRouterSpecIds: str):
    """
    全局共享VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecIds: VPC路由器规格ID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5606
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vpcRouterSpecIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalShareVpcRouterSpec.value, data)


def globalRecallVpcRouterSpec(accessToken, vpcRouterSpecIds: str):
    """
    全局召回VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecIds: VPC路由器规格ID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5607
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vpcRouterSpecIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.globalRecallVpcRouterSpec.value, data)


def deleteVpcRouterSpec(accessToken, vpcRouterSpecIds: str):
    """
    删除VPC路由器规格
    :param accessToken:
    :param vpcRouterSpecIds: VPC路由器规格ID(多个使用英文逗号分割)
    :return:
    """
    operationType = 5603
    data = _getOperationType(operationType, AsyncType.SYNC.value)
    data['ids'] = vpcRouterSpecIds
    return get_utils(accessToken).cloud_http_request.post(networkUrls.deleteVpcRouterSpec.value, data)


def getVpcRouterSpecMirror(accessToken, status=1, notFormat='', **kwargs):
    """
    查询VPC路由器规格镜像信息
    :param accessToken:
    :param status: 路由器镜像状态
                        1:启用
                        0:停用
    :param notFormat: 排除的路由器镜像格式
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVpcRouterSpecMirror.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'status': status,
                                                             'notformat': notFormat,
                                                         })


def getVpcRouterSpecNetwork(accessToken, networkType: int, clusterId='', **kwargs):
    """
    查询VPC路由器规格管理网络信息
    :param accessToken:
    :param networkType: 查询网络类型
                            1：公有网络
                            2: 管理网络
    :param clusterId:
    :param kwargs: limit:
                   offset:
                   keyword: xxx
    :return:
    """
    return get_utils(accessToken).cloud_http_request.get(networkUrls.getVpcRouterSpecNetwork.value,
                                                         {
                                                             'offset': kwargs.get('offset', OFFSET),
                                                             'limit': kwargs.get('limit', LIMIT),
                                                             'keyword': kwargs.get('keyword', ''),
                                                             'type': networkType,
                                                             'clusterid': clusterId,
                                                         })
